package com.xiguaji.tiktok.task.blogger.event;

import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import com.gugee.tiktok.common.model.dal.blogger.BloggerLogRepository;
import com.gugee.tiktok.common.model.dal.blogger.BloggerMainRepository;
import com.gugee.tiktok.common.model.mysql.BloggerLog;
import com.gugee.tiktok.common.model.mysql.BloggerMain;
import com.xiguaji.boot.eventbus.Event;
import com.xiguaji.boot.eventbus.EventBusTemplate;
import com.xiguaji.boot.eventbus.IEventListener;
import com.xiguaji.tiktok.task.common.config.AppConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Optional;

@Component
public class RefreshBloggerRecordEventListener implements IEventListener<RefreshBloggerRecordEventListener.RefreshBloggerRecordEvent> {
    @Autowired
    BloggerLogRepository bloggerLogRepository;

    @Autowired
    BloggerMainRepository bloggerMainRepository;

    @Autowired
    @Lazy
    EventBusTemplate eventBusTemplate;
    @Autowired
    AppConfig appConfig;
    @Override
    @Subscribe
    @AllowConcurrentEvents
    public void handle(RefreshBloggerRecordEventListener.RefreshBloggerRecordEvent event) {
        run(event);
    }

    public void run(RefreshBloggerRecordEventListener.RefreshBloggerRecordEvent event) {
        int bloggerLogType = BloggerLog.BloggerLogType.REFRESH_BLOGGER_RECORD_LOOP.getValue();

        Optional<BloggerLog> bloggerLogOptional = bloggerLogRepository.findByLogType(bloggerLogType);
        BloggerLog bloggerLog;

        int page = 1;
        if (bloggerLogOptional.isPresent()) {
            page = (int)bloggerLogOptional.get().getCurrentBloggerId();
            bloggerLog = bloggerLogOptional.get();
        } else {
            bloggerLog = new BloggerLog();
            bloggerLog.setLogType(bloggerLogType);
//            bloggerLog.setExtValue("");
            bloggerLog.setCreateBy(event.getSource());
            bloggerLog.setUpdateBy(event.getSource());
        }
        /**
         * todo 打点的播主可能是自定义进来的，需要区分表存储。不可借用原有的逻辑走,原有逻辑需要重构消除
         */
        List<BloggerMain> bloggers = bloggerMainRepository.findBloggersByPageAndCount((page-1)*3000, 3000);
        if (CollectionUtils.isEmpty(bloggers)) {
            bloggerLog.setCurrentBloggerId(1);
            bloggerLogRepository.save(bloggerLog);
            return;
        }

        bloggerLog.setCurrentBloggerId(page+1);
        bloggerLogRepository.save(bloggerLog);

        var saveBloggerRecordEvent = new SaveBloggerRecordEventListener.SaveBloggerRecordEvent(event,bloggers);
        eventBusTemplate.post(saveBloggerRecordEvent);
    }

    public static class RefreshBloggerRecordEvent extends Event {
        public RefreshBloggerRecordEvent() {
            super("RefreshBloggerRecord");
        }
    }
}
